package br.com.cinesystem.persistence;

import br.com.cinesystem.entity.Cinema;
import br.com.cinesystem.entity.Sala;
import java.util.ArrayList;
import java.util.List;

public class SalaDao extends Dao{

    public SalaDao() {
    }


    public void salvar(Sala s) throws Exception{
        abrirConexao();

        stmt = con.prepareStatement("insert into sala values(null,?,?,?,1)");
        stmt.setString(1, s.getNome());
        stmt.setInt(2, s.getCinema().getIdCinema());
        stmt.setInt(3, s.getCapacidade());

        stmt.execute();

        fecharConexao();
    }

    public boolean nomeExistente(Sala s) throws Exception{
        abrirConexao();

        stmt = con.prepareStatement("select * from sala where nome=? and id_Cinema=?");
        stmt.setString(1, s.getNome());
        stmt.setInt(2, s.getCinema().getIdCinema());

        rs = stmt.executeQuery();

        boolean b = rs.next();

        fecharConexao();
        return b;
    }

    public List<Sala> buscarTodos()throws Exception{

        abrirConexao();

        StringBuilder sb = new StringBuilder();
        sb.append("select c.idCinema, c.razaoSocial, ");
        sb.append("s.idSala, s.capacidade, s.nome ");
        sb.append("from cinema c inner join sala s ");
        sb.append("on c.idCinema = s.id_Cinema ");
        sb.append("WHERE s.situacao=1 and c.situacao=1 ORDER BY idCinema, nome");

        stmt = con.prepareStatement(sb.toString());

        rs = stmt.executeQuery();

        List<Sala> lista = new ArrayList<Sala>();

        while (rs.next()){

            Sala sala = new Sala();
            sala.setCinema(new Cinema());

            sala.setIdSala(rs.getInt("idSala"));
            sala.setNome(rs.getString("nome"));
            sala.setCapacidade(rs.getInt("capacidade"));
            sala.getCinema().setIdCinema(rs.getInt("idCinema"));
            sala.getCinema().setRazaoSocial(rs.getString("razaoSocial"));


            lista.add(sala);
        }

        fecharConexao();
        return lista;
    }

    public Sala buscar(Sala s) throws Exception{

        abrirConexao();

        StringBuilder sb = new StringBuilder();
        sb.append("select c.idCinema, c.razaoSocial, ");
        sb.append("s.idSala, s.capacidade, s.nome ");
        sb.append("from cinema c inner join sala s ");
        sb.append("on c.idCinema = s.id_Cinema ");
        sb.append("WHERE s.idSala=?");

        stmt = con.prepareStatement(sb.toString());
        stmt.setInt(1, s.getIdSala());

        rs = stmt.executeQuery();

        Sala sala = null;

        if(rs.next()){

            sala = new Sala();
            sala.setCinema(new Cinema());

            sala.setIdSala(rs.getInt("idSala"));
            sala.setNome(rs.getString("nome"));
            sala.setCapacidade(rs.getInt("capacidade"));
            sala.getCinema().setIdCinema(rs.getInt("idCinema"));
            sala.getCinema().setRazaoSocial(rs.getString("razaoSocial"));

        }

        fecharConexao();
        return sala;
    }


    public void alterar(Sala s) throws Exception{

        abrirConexao();

        stmt = con.prepareStatement("UPDATE sala SET id_Cinema=?, nome=?, capacidade=? WHERE idSala=?");

        stmt.setInt(1, s.getCinema().getIdCinema());
        stmt.setString(2, s.getNome());
        stmt.setInt(3, s.getCapacidade());
        stmt.setInt(4, s.getIdSala());

        stmt.execute();

        fecharConexao();

    }

    public void excluir(Sala s) throws Exception{

        abrirConexao();

        stmt = con.prepareStatement("UPDATE sala SET situacao=0 WHERE idSala=?");

        stmt.setInt(1, s.getIdSala());

        stmt.execute();

        fecharConexao();

    }


}
